/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hmvc4gwt.client.component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import hmvc4gwt.client.controller.IController;
import hmvc4gwt.client.event.IEvent;
import hmvc4gwt.client.event.IEventListener;
import hmvc4gwt.client.model.IModel;
import hmvc4gwt.client.view.IView;

/**
 * This AComponent implementation stores its child components in a List and adds
 * an child-components only one time, any other IComponent.addChild()-method-call
 * returns the first added child-component.
 * 
 * @author jens.meiss
 *
 */
public class ComponentSingleChild extends AComponent {

	/**
	 * contains all child-components
	 */
	private Map mapChildComponents = new HashMap(5);	
	
	/**
	 * creates an Controller-Child-Component
	 * @param parentComponent
	 * @param controller
	 */
	public ComponentSingleChild(IComponent parentComponent,
			IController controller) {
		createComponent(parentComponent, controller);
	}

	/**
	 * creates an Controller-Model-Child-Component
	 * @param parentComponent
	 * @param controller
	 * @param model
	 */
	public ComponentSingleChild(IComponent parentComponent,
			IController controller, IModel model) {
		createComponent(parentComponent, controller, model);
	}
	
	/**
	 * creates an Controller-Model-View-Child-Component
	 * @param parentComponent
	 * @param controller
	 * @param model
	 * @param view
	 */
	public ComponentSingleChild(IComponent parentComponent,
			IController controller, IModel model, IView view) {
		createComponent(parentComponent, controller, model, view);
	}

	/**
	 * creates an Controller-View-Child-Component
	 * @param parentComponent
	 * @param controller
	 * @param view
	 */
	public ComponentSingleChild(IComponent parentComponent,
			IController controller, IView view) {
		createComponent(parentComponent, controller, view);
	}

	/**
	 * creates an Controller-Component
	 * @param controller
	 */
	public ComponentSingleChild(IController controller) {
		createComponent(controller);
	}

	/**
	 * creates an Controller-Model-Component
	 * @param controller
	 * @param model
	 */
	public ComponentSingleChild(IController controller, IModel model) {
		createComponent(controller, model);
	}
	
	/**
	 * creates an Controller-Model-View-Component
	 * @param controller
	 * @param model
	 * @param view
	 */
	public ComponentSingleChild(IController controller, IModel model, IView view) {
		createComponent(controller, model, view);
	}

	/**
	 * creates an Controller-View-Component
	 * @param controller
	 * @param view
	 */
	public ComponentSingleChild(IController controller, IView view) {
		createComponent(controller, view);
	}

	protected IComponent addChildComponent(IComponent component) {
		
		Class classOfController = component.getController().getClassOfObject();
		
		if (mapChildComponents.containsKey(classOfController)) {
			return (IComponent) mapChildComponents.get(classOfController);
		} else {
			mapChildComponents.put(classOfController, component);
			return component;
		}
		
	}

	public IComponent addChildComponent(IController controller) {
		return addChildComponent(new ComponentSingleChild(this,
				controller));
	}

	public IComponent addChildComponent(IController controller, IModel model) {
		return addChildComponent(new ComponentSingleChild(this,
				controller, model));
	}

	public IComponent addChildComponent(IController controller, IModel model,
			IView view) {
		return addChildComponent(new ComponentSingleChild(this,
				controller, model, view));
	}

	public IComponent addChildComponent(IController controller, IView view) {		
		return addChildComponent(new ComponentSingleChild(this, controller, view));
	}

	protected void callLocalListeners(IEvent event) {
		
		/*
		 * exsitieren Listener
		 */
		if (getMapEventListener().containsKey(event.getClassOfObject())) {
			
			/*
			 * über die Liste der Listener iterieren
			 */
			List listListener = (List) getMapEventListener().get(event.getClassOfObject());
			Iterator iteratorListener = listListener.iterator();
			
			while (iteratorListener.hasNext()) {
				IEventListener listener = (IEventListener) iteratorListener.next();
				listener.handleEvent(event);
			}
			
		}
		
	}

	public boolean containsChildComponent(IComponent component) {
		return mapChildComponents.containsKey(component.getController().getClassOfObject());
	}

	public void fireEvent(IEvent event, boolean up, boolean local,
			boolean down) {
		
		if (down) {
			/*
			 * send the Event to all child Controller
			 */
			Iterator iteratorChildComponents = mapChildComponents.entrySet().iterator();
			
			while (iteratorChildComponents.hasNext()) {
				
				Map.Entry entry = (Map.Entry) iteratorChildComponents.next();
				
				IComponent component = (IComponent) entry.getValue();
				component.fireEvent(event, false, true, true);
			}
		}

		/*
		 * don't call localListeners, the Component is shutting down
		 */
		if (local && (isTerminated() == false)) {
			/*
			 * call all local Listeners of the Event
			 */
			callLocalListeners(event);
		}

		if (up) {
			/*
			 * send the Event to all parent Controller
			 */
			if (getParentComponent() != null) {
				getParentComponent().fireEvent(event, true, true, false);
			}
		}
	}

	protected void terminateChildComponents() {

		Iterator iterator = mapChildComponents.entrySet().iterator();

		while (iterator.hasNext()) {
			
			Map.Entry entry = (Map.Entry) iterator.next();
			
			IComponent component = (IComponent) entry.getValue(); 
			component.terminate();
		}
	}
	
	public void removeTerminatedChildComponents() {
		Iterator iterator = getListTerminatedChildComponents().iterator();
		while (iterator.hasNext()) {
			IComponent component = (IComponent) iterator.next();
			mapChildComponents.remove(component.getController().getClassOfObject());
		}
		
		getListTerminatedChildComponents().clear();
	}

}
